import CommonBaseSchema from '#common/base_schema'

export default class extends CommonBaseSchema {
  protected tableName = 'users'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments()

      table
        .integer('fk_shop_id')
        .notNullable()
        .unsigned()
        .references('shops.id')
        .comment('所属店铺ID')

      table.integer('fk_role_id').unsigned().nullable().references('dicts.id').comment('角色ID')

      table
        .integer('fk_source_id')
        .unsigned()
        .nullable()
        .references('dicts.id')
        .comment('数据来源ID')

      table.integer('fk_dept_id').unsigned().nullable().references('dicts.id').comment('部门ID')

      table.integer('fk_post_id').unsigned().nullable().references('dicts.id').comment('岗位ID')

      table.string('user_name', 15).notNullable().comment('用户名')
      table.string('password', 32).defaultTo('e10adc3949ba59abbe56e057f20f883e').comment('密码')
      table.string('nick_name', 20).nullable().comment('昵称')
      table.string('phone', 11).defaultTo('').comment('手机号码')
      table.string('openid', 100).nullable().comment('OpenID')
      table.string('email', 50).defaultTo('').comment('电子邮箱')
      table.string('avatar_url', 300).nullable().comment('头像链接')
      table.integer('sex').defaultTo(2).unsigned().comment('性别 0女 1男 2未知')
      table.tinyint('status').defaultTo(1).unsigned().comment('状态')
      table.string('keywords', 100).nullable().comment('关键字')
      table.integer('login_shop_id').defaultTo(1).unsigned().comment('登录店铺ID')
      table.string('memo', 255).defaultTo('').comment('备注')

      this.defineCommonColumns(table)

      table.unique(['user_name', 'phone', 'openid', 'keywords'])
      table.comment('用户表')
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
